package org.scau.studentevaluation.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import lombok.extern.slf4j.Slf4j;
import org.scau.studentevaluation.util.LogConstants;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

/**
 * @Author: x1aolone
 * @Date: 2020/8/19 11:39
 * 与AccessLogFilter配合使用，记录服务端接受到请求的时间
 * Zuul相关的注释在PreRequestFilter中
 */
@Slf4j
@Component
public class PreRequestFilter extends ZuulFilter {

    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        RequestContext requestContext = RequestContext.getCurrentContext();
        Long startTime = System.currentTimeMillis();
        requestContext.set(LogConstants.START_TIME_Millis,startTime);
        return null;
    }
}
